package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;
import com.stark.calculator.unit.constant.ConvertType;
import com.stark.calculator.unit.model.UnitBean;
import com.stark.calculator.unit.model.UnitDataProvider;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityLengthConversionBinding;
import stark.common.basic.utils.Str2NumUtil;

public class LengthConversionActivity extends BaseAc<ActivityLengthConversionBinding> {

    private Dialog myUnitDialog;
    private WheelView wheelView;
    private int type;
    private List<UnitBean> mAllUnitBean;
    private UnitBean mBaseUnitBean, newUnitbean;


    @Override
    protected int onCreate() {
        return R.layout.activity_length_conversion;
    }

    @Override
    protected void initView() {
        mDataBinding.ivLengthConversionBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvBaseUnit.setOnClickListener(this);
        mDataBinding.tvResultUnit.setOnClickListener(this);
        mDataBinding.tvConvert.setOnClickListener(this);
        mDataBinding.tvReset.setOnClickListener(this);

        mDataBinding.etBaseValue.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        if (mAllUnitBean == null) {
            mAllUnitBean = UnitDataProvider.getDatas(ConvertType.LENGTH);
        }
        mBaseUnitBean = mAllUnitBean.get(0);
        newUnitbean = mAllUnitBean.get(0);
        mDataBinding.tvBaseUnit.setText(mBaseUnitBean.cnName);
        mDataBinding.tvResultUnit.setText(newUnitbean.cnName);

        //选择单位弹框
        selUnitDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvBaseUnit://初始单位
                type = 0;
                myUnitDialog.show();
                break;
            case R.id.tvResultUnit://换算单位
                type = 1;
                myUnitDialog.show();
                break;
            case R.id.tvConvert://转换
                convert();
                break;
            case R.id.tvReset://重置
                mDataBinding.etBaseValue.setText("");
                mDataBinding.tvResultValue.setText("");
                mBaseUnitBean = mAllUnitBean.get(0);
                newUnitbean = mAllUnitBean.get(0);
                mDataBinding.tvBaseUnit.setText(mBaseUnitBean.cnName);
                mDataBinding.tvResultUnit.setText(newUnitbean.cnName);
                break;
            //选择单位弹框
            case R.id.tvDialogUnitCancel://取消
                myUnitDialog.dismiss();
                break;
            case R.id.tvDialogUnitRight://确定
                myUnitDialog.dismiss();
                if (type == 0) {
                    mBaseUnitBean = mAllUnitBean.get(wheelView.getCurrentItem());
                    mDataBinding.tvBaseUnit.setText(mBaseUnitBean.cnName);
                } else if (type == 1) {
                    newUnitbean = mAllUnitBean.get(wheelView.getCurrentItem());
                    mDataBinding.tvResultUnit.setText(newUnitbean.cnName);
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void convert() {
        String input = mDataBinding.etBaseValue.getText().toString();
        double value = Str2NumUtil.parse(input);
        double newValue = ((1 * newUnitbean.baseRateValue) / mBaseUnitBean.baseRateValue) * value;
        newUnitbean.value = String.valueOf(newValue);
        mDataBinding.tvResultValue.setText(newUnitbean.value);
    }


    //选择单位弹框
    private void selUnitDialog() {
        myUnitDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_unit, null);
        myUnitDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myUnitDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogUnitCancel);
        TextView dialogRight = inflate.findViewById(R.id.tvDialogUnitRight);
        wheelView = inflate.findViewById(R.id.wheel_view1);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
        wheelView.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView.setTextColorCenter(Color.parseColor("#0095FF"));
        wheelView.setLineSpacingMultiplier(2.5f);
        wheelView.setDividerType(WheelView.DividerType.WRAP);
        wheelView.setCurrentItem(0);
        wheelView.setItemsVisibleCount(4);
        wheelView.setCyclic(false);
        wheelView.setTextSize(18);
        //设置滚轮数据


        List<String> listUnit = new ArrayList<>();
        for (UnitBean unitBean : mAllUnitBean) {
            listUnit.add(unitBean.cnName);
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listUnit);
        wheelView.setAdapter(adapter1);
    }

}